/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.mq.redis;


import cn.ac.iie.di.wx.mq.utils.RedisUtil;
import cn.ac.iie.di.wx.mq.vo.RedisVo;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;


/**
 *
 * @author Administrator
 */
public class RedisThread implements Runnable {

    public RedisUtil redisUtil;
    public LinkedBlockingQueue<RedisVo> redisVos;
    static Logger logger = Logger.getLogger(RedisThread.class.getName());

    public RedisThread(RedisUtil redisUtil, LinkedBlockingQueue<RedisVo> redisVos) {
        this.redisUtil = redisUtil;
        this.redisVos = redisVos;
    }

    public LinkedBlockingQueue<RedisVo> getRedisVos() {
        return redisVos;
    }

    public void setRedisVos(LinkedBlockingQueue<RedisVo> redisVos) {
        this.redisVos = redisVos;
    }

    public RedisUtil getRedisUtil() {
        return redisUtil;
    }

    public void setRedisUtil(RedisUtil redisUtil) {
        this.redisUtil = redisUtil;
    }

    @Override
    public void run() {
        while (true) {
            try {
                RedisVo redisVo = redisVos.poll(1, TimeUnit.SECONDS);
                if (redisVo != null) {
                    String opid = redisUtil.get(redisVo.getKey());
                    if (opid!=null) {
                        logger.info("opid is existed:"+opid);
                    }
                    String result = redisUtil.set(redisVo.getKey(), redisVo.getValue());
                    logger.info("set into redis is "+result+",opid is "+redisVo.getKey()+",value is "+ redisVo.getValue());
//                    if (!result.equals("OK")) {
//                        RuntimeEnv.retryQueue.add(redisVo);
//                        logger.info("set "+opid+" redis "+result+",will retry");
//                    }
                }
                redisVo = null;
                Thread.sleep(100);
            } catch (InterruptedException ex) {
                logger.info(ex.getMessage(), ex);
            }

        }
    }

}
